/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Dawn
 */
@WebServlet(name="Registration", urlPatterns={"/Registration"})
public class Registration extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Statement stm=null;
        Connection connection = null;

        String Username = request.getParameter("Username");
        String Password = request.getParameter("Password");
        String Retype = request.getParameter("ConfirmPassword");
        String Nama = request.getParameter("Name");
        String Email = request.getParameter("EMail");
        
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Registration Processing</title>");

            try{
                try {
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                } catch (InstantiationException ex) {
                    Logger.getLogger(LoginProcess.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(LoginProcess.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            catch (ClassNotFoundException ex){
            }

        }
        finally {
            if (Nama.equals("") || Username.equals("") || Password.equals("") || Retype.equals("") || Email.equals(""))
            {
                response.sendRedirect("index.jsp?s=3");
            }
            else
            {
                if (Retype.equals(Password))
                {
                    connection = DriverManager.getConnection("jdbc:mysql://localhost/chatter?user=root&password=");

                    stm=connection.createStatement();
                    stm.execute("INSERT INTO `users` (`Nama`, `Username`, `Password`, `Email`, `MaxRoom`, `Role`, `Banned`) VALUES ('"+ Nama +"', '"+ Username +"', '"+ Password +"', '"+ Email +"', 5, 'User', 0)");

                    response.sendRedirect("index.jsp?s=2"); // Main function

                    connection.close();

                    stm.close();
                    out.close();
                }
                else
                {
                    response.sendRedirect("index.jsp?s=999");
                }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
